package com.jrx.anytxn.param.controller.limit;

import com.google.common.collect.Maps;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.param.dto.limit.CreditLimitTemplateReq;
import com.jrx.anytxn.param.dto.limit.CreditLimitTemplateRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.limit.ICreditLimitTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author by yf
 * @Classname CreditLimitTemplateController
 * @Description 额度模板表
 * @Date 2019/6/12 0012 下午 21:09
 */
@Api(tags = "页面相关的额度模板表参数API接口")
@RestController
@RequestMapping("/param/limitTemplates")
public class CreditLimitTemplateController {
    private static final Logger logger = LoggerFactory.getLogger(CreditLimitTemplateController.class);

    @Autowired
    private InfoService infoService;
    @Autowired
    private ICreditLimitTemplateService iCreditLimitTemplateService;

    @ApiOperation(value = "根据租户ID分页查询额度模板表信息", notes = "根据租户ID分页查询额度模板表信息")
    @GetMapping("/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<CreditLimitTemplateRes>> getAllLimitTemplateByPage(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize) {
        TxnRespResult<TxnPage<CreditLimitTemplateRes>> result = new TxnRespResult<>();
        TxnPage<CreditLimitTemplateRes> response;
        try {
            String tenantId = String.valueOf(infoService.getUser().getProjectId());
            response = iCreditLimitTemplateService.findAllPageByTenId(pageNum, pageSize, tenantId);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据额度模板ID和组织ID删除额度模板信息", notes = "根据额度模板ID和组织ID删除额度模板信息")
    @DeleteMapping(value = "/templateId/{templateId}/tenantId/{tenantId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateId", value = "额度模板ID", required = true),
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true)})
    public TxnRespResult<Boolean> removeByTempIdAndOrgId(@PathVariable(value = "templateId") String templateId, @PathVariable(value = "tenantId") String tenantId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);


            flag = iCreditLimitTemplateService.removeLimitTemplateByTempIdAndTenId(templateId, String.valueOf(infoService.getUser().getProjectId()));
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }

    @ApiOperation("通过额度模板ID和组织ID查某一特定详细信息")
    @GetMapping("/templateId/{templateId}/tenantId/{tenantId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateId", value = "额度模板ID", required = true),
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true)})
    public TxnRespResult<CreditLimitTemplateRes> getByTemIdAndOrgId(@PathVariable(value = "templateId") String templateId, @PathVariable(value = "tenantId") String tenantId) {
        TxnRespResult<CreditLimitTemplateRes> result = new TxnRespResult<>();
        try {
            CreditLimitTemplateRes res = iCreditLimitTemplateService.findByTemplateIdAndTenId(templateId, String.valueOf(infoService.getUser().getProjectId()));
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result;
    }

    @ApiOperation("添加额度模板信息")
    @PostMapping("/")
    public TxnRespResult<CreditLimitTemplateRes> createLimitTemplate(@RequestBody CreditLimitTemplateReq creditLimitTemplateReq) {
        TxnRespResult result = new TxnRespResult();
        try {
            creditLimitTemplateReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            creditLimitTemplateReq.setUpdateBy(infoService.getUser().getUserName());
            CreditLimitTemplateRes creditLimitTemplateRes = iCreditLimitTemplateService.addLimitTemplate(creditLimitTemplateReq);
            result.getSuccess(creditLimitTemplateRes);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation("编辑热度模板信息")
    @PutMapping("/")
    public TxnRespResult<CreditLimitTemplateRes> modifyLimitTemplate(@RequestBody CreditLimitTemplateReq creditLimitTemplateReq) {
        TxnRespResult<CreditLimitTemplateRes> result = new TxnRespResult<>();
        try {
            creditLimitTemplateReq.setUpdateBy(infoService.getUser().getUserName());
            CreditLimitTemplateRes creditLimitTemplateRes = iCreditLimitTemplateService.modifyLimitTemplate(creditLimitTemplateReq);
            result.getSuccess(creditLimitTemplateRes);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "获得额度模板表信息", notes = "获得额度模板表信息")
    @GetMapping(value = "/list")
    public TxnRespResult<HashMap<String, List<CreditLimitTemplateRes>>> getCreditLimitTemplateList() {
        TxnRespResult<HashMap<String, List<CreditLimitTemplateRes>>> result = new TxnRespResult<>();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        List<CreditLimitTemplateRes> allCreditLimitTemplate = iCreditLimitTemplateService.findAllLimitTemplateByTenId(tenantId);

        HashMap<String, List<CreditLimitTemplateRes>> sysCodeMap = Maps.newHashMap();
        sysCodeMap.put("listData", allCreditLimitTemplate);
        return result.getSuccess(sysCodeMap);
    }


}
